/**
 * Created by wanghuan on 2017/4/9.
 */

/*==========================Employee 相关接口==============================*/

import Ajax from '../../apiAjax';

export  default {
  state:{
    employees:{},//员工列表
    employeeDetail:{},
    employeeForm:{},
    employeeOvertimeRule:{},
    allEmployeeList:[],
  },
  mutations:{
    setCommonStateData(state,{key,data}){
      state[key] = data;
    }
  },
  actions:{
    /**
     *查询Employee列表
     * @param commit
     * @param paramData = {
     * "page": 4, //当前页码
    "   pageSize": 10 //每页的记录数
     * }
     * @returns {Promise}
     */
    getEmployees({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/list',
          data:paramData,
          success:function (data) {
            console.log(data)
            commit('setCommonStateData',{key:'employees',data:data.data});
            resolve(data);
          }
        })
      })
    },
    /**
     * 删除员工
     * @param commit
     * @param paramData{id：12}
     * @returns {Promise}
     */
    delEmployee({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/del',
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    /**
     * 查询员工详情
     * @param commit
     * @param paramData{id}
     * @returns {Promise}
     */
    getEmployeeDetail({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/getEmployee',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'employeeDetail',data:data.data});
            resolve(data);
          }
        })
      })
    },
    getEmployeeForm({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/toForm',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'employeeForm',data:data.data});
            resolve(data);
          }
        })
      })
    },
    /**
     * 新增员工
     * @param commit
     * @param paramData
     * @returns {Promise}
     */
    saveEmployee({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/addEmployee',
        /*  contentType: "application/json;charset=UTF-8",*/
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    updateEmployee({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/updateEmployee ',
          /*  contentType: "application/json;charset=UTF-8",*/
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    saveJobInfo({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/saveJobInfo',
          /*  contentType: "application/json;charset=UTF-8",*/
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    deleteJobInfo({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/delJob',
          /*  contentType: "application/json;charset=UTF-8",*/
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    updateJobInfo({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/updateJobInfo',
          /*  contentType: "application/json;charset=UTF-8",*/
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    getEmployeeOverTimeRule({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/time/overtimeRule/getOvertimeRule',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'employeeOvertimeRule',data:data.data});
            resolve(data);
          }
        })
      })
    },
    switchEmployeeOvertimeRule({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/time/overtimeRule/switchOvertimeRule',
          data:paramData,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    updateEmployeeHeaderImage({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/updateHeadImg',
          data:paramData,
          dataType:'json',
          type:'post',
          processData: false,
          contentType: false,
          success:function (data) {
            resolve(data);
          }
        })
      })
    },
    getAllEmployeeList({commit},paramData){
      return new Promise((resolve, reject) => {
        Ajax({
          url:'/employee/allList',
          data:paramData,
          success:function (data) {
            commit('setCommonStateData',{key:'allEmployeeList',data:data.data});
            resolve(data);
          }
        })
      })
    },
  },
  getters: {
    employees:(state, getters)=> {
      return state.employees;
    },
    employeeDetail:(state, getters)=> {
      return state.employeeDetail;
    },
    employeeForm:(state, getters)=> {
      return state.employeeForm;
    },
    employeeOvertimeRule:(state, getters)=> {
      return state.employeeOvertimeRule;
    },
    allEmployeeList:(state, getters)=> {
      return state.allEmployeeList;
    },
  }
}
